import React, { Component } from 'react'
import { Switch, Route, Redirect } from 'react-router-dom'
//显示路由 配置路由组件
export default class RouterView extends Component {
  render () {
    //console.log(this.props, 'route')
    const { routes } = this.props
    console.log(routes, 'viewprops')
    return (
      <div>
        {/* 接收路由表参数 渲染一级路由 */}
        <Switch>
          {
            //重定向
            routes.map((item, index) => {
              if (item.from) {
                return <Redirect key={index} from={item.from} to={item.to} />
              }
              if (item.to === '/404') {
                return <Redirect key={index} to={item.to} />
              }
              return <Route key={index} path={item.path} render={(routeParams) => {
                //一级 component={item.component}
                // return <item.component routesChildren={item.children} />
                const Component = item.component
                // {...routeParams}传递路由信息对象
                return <Component {...routeParams} routesChildren={item.children} />
              }}></Route>
            })
          }
        </Switch>
      </div>
    )
  }
}
